//
//  YRJDetectionNetworkStatusTool.m
//  YanRenjie
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJDetectionNetworkStatusTool.h"

static NSString * const AFAppDotNetAPIBaseURLString = @"www.baidu.com";
@implementation YRJDetectionNetworkStatusTool


+ (instancetype)sharedClient {
    static YRJDetectionNetworkStatusTool *_sharedClient = nil;
    static dispatch_once_t onceToken;
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    dispatch_once(&onceToken, ^{
        _sharedClient = [[YRJDetectionNetworkStatusTool alloc] initWithBaseURL:[NSURL URLWithString:AFAppDotNetAPIBaseURLString]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        
        [_sharedClient.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusReachableViaWWAN:
                    NSLog(@"-------AFNetworkReachabilityStatusReachableViaWWAN------");
                    UIAlertView *alerView2 = [[UIAlertView alloc] initWithTitle:@"警告" message:@"本引用耗流量非常大请切换WIFI" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alerView2 show];
                    _sharedClient.isStatus = YES;
                    break;
                    
                case AFNetworkReachabilityStatusReachableViaWiFi:
                    NSLog(@"-------AFNetworkReachabilityStatusReachableViaWiFi------");
                    UIAlertView *alerView1 = [[UIAlertView alloc] initWithTitle:@"警告" message:@"正在使用WIFI请放心使用" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [center postNotificationName:@"wlan" object:@"2" userInfo:nil];
                    [alerView1 show];
                    _sharedClient.isStatus = YES;
                    break;
                    
                case AFNetworkReachabilityStatusNotReachable:
                    NSLog(@"-------AFNetworkReachabilityStatusNotReachable------");
                    
                    UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"网络异常" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alerView show];
                    [center postNotificationName:@"wlan" object:@"1" userInfo:nil];
                    _sharedClient.isStatus = NO;
                    break;
                    
                default:
                    break;
            }
        }];
        [_sharedClient.reachabilityManager startMonitoring];
    });
    return _sharedClient;
}

@end
